package org.pcorp.battlespace.construction.ui.tools;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;

import org.pcorp.battlespace.construction.ui.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DataCellEditor extends DefaultCellEditor {
	private static final long serialVersionUID = 7079046498835815437L;
	private static final Logger _log = LoggerFactory.getLogger(DataCellEditor.class);
	
	public DataCellEditor() {
		super(new JTextField());
		final JTextField textField = (JTextField) getComponent();
		
		textField.addFocusListener(new FocusAdapter() { 
			@Override
			public void focusGained(FocusEvent e) {
				textField.selectAll();}
		});
	}
	
	public DataCellEditor(Principal p) {
		super(new JTextField());
		final Principal princ = p;
		
		final JTextField textField = (JTextField) getComponent();
		textField.setBackground(ConstanteUI.BACKGROUND_COLOR);
		textField.setForeground(ConstanteUI.EDIT_COLOR);
		textField.setBorder(BorderFactory.createEmptyBorder());
		
		textField.addFocusListener(new FocusAdapter() { 
			@Override
			public void focusGained(FocusEvent e) {
				textField.selectAll();}
			
			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				_log.debug("focus lost");
				princ.updateModel(null);
				princ.updateDisplay(null);
				
			}
		});
		
	}
	
	
}
